$(function () {
    $("#showInsertTeacherDialog").click(showInsertTeacherDialog);
    $("#insertTeacher").click(insertTeacher);
    $("#updateTeacher").click(updateTeacher);
    $("#queryTeacherButton").click(queryTeacher);
    $("#resetTeacherButton").click(resetTeacherButton);
    fillPageDataListTable();
    fillQueryPageDataListClasszSelect();
    initOrganizationInfo();
    initQueryTeacherSelectChange();

    layui.use(['laydate'], function () {
        var laydate = layui.laydate;
        //日期时间选择器
        laydate.render({
            elem: '#queryBeginBirthday',
            type: 'date'
        });

        laydate.render({
            elem: '#queryEndBirthday',
            type: 'date'
        });

        laydate.render({
            elem: '#queryBeginRegisterDate',
            type: 'date'
        });

        laydate.render({
            elem: '#queryEndRegisterDate',
            type: 'date'
        });

        laydate.render({
            elem: '#insertBirthday',
            type: 'date'
        });

        laydate.render({
            elem: '#editBirthday',
            type: 'date'
        });
    });
})

function initQueryTeacherSelectChange() {
    layui.use(['form'], function () {
        var form = layui.form;
        form.on('select(queryClasszSelect)', function (data) {
            fillPageDataListTable();
        });
        form.on('select(sex)', function (data) {
            fillPageDataListTable();
        });
    });
}

function fillQueryPageDataListClasszSelect() {
    $.post("/classzapplicationform/queryClasszListByOrganizationId?t=" + new Date().getTime(), function (data) {
        var jsonObject = JSON.parse(data);
        if (jsonObject.responseCode == 500) {
            layer.msg(jsonObject.message);
        }
        if (jsonObject.responseCode == 501) {
            layer.msg("系统级异常：" + jsonObject.message);
        }
        if (jsonObject.responseCode == 200) {
            var getClasszList = jsonObject.responseData;
            for (var i = 0; i < getClasszList.length; i++) {
                var eachClassz = getClasszList[i];
                var classzId = eachClassz.id;
                var classzName = eachClassz.classzName;

                var newOption = $("<option></option>");
                $(newOption).text(classzName);
                $(newOption).val(classzId);

                $("#queryTeacherTable select[name='queryClasszSelect']").append(newOption);
            }

            layui.use('form', function () {
                var form = layui.form;
                form.render();
            });
        }
    });
}

function queryTeacher() {
    $("#queryTeacherTable input[type='hidden'][name='gotoPageHidden']").val("1")
    fillPageDataListTable();
}

function resetTeacherButton() {
    $("#queryTeacherTable :text[name='teacherId']").val("");
    $("#queryTeacherTable :text[name='teacherName']").val("");
    $("#queryTeacherTable :text[name='queryBeginBirthday']").val("");
    $("#queryTeacherTable :text[name='queryEndBirthday']").val("");
    $("#queryTeacherTable select[name='sex']").val("-1");
    $("#queryTeacherTable :text[name='phone']").val("");
    $("#queryTeacherTable :text[name='email']").val("");
    $("#queryTeacherTable :text[name='queryBeginRegisterDate']").val("");
    $("#queryTeacherTable :text[name='queryEndRegisterDate']").val("");
    $("#queryTeacherTable select[name='queryClasszSelect']").val("-1");
    $("#queryTeacherTable input[type='hidden'][name='gotoPageHidden']").val("1");

    layui.use('form', function () {
        var form = layui.form;
        form.render();
    });

    queryTeacher();
}

function fillPageDataListTable() {
    var teacherId = $("#queryTeacherTable :text[name='teacherId']").val();
    var teacherName = $("#queryTeacherTable :text[name='teacherName']").val();
    var queryBeginBirthday = $("#queryTeacherTable :text[name='queryBeginBirthday']").val();
    var queryEndBirthday = $("#queryTeacherTable :text[name='queryEndBirthday']").val();
    var sex = $("#queryTeacherTable select[name='sex']").val();
    var phone = $("#queryTeacherTable :text[name='phone']").val();
    var email = $("#queryTeacherTable :text[name='email']").val();
    var queryBeginRegisterDate = $("#queryTeacherTable :text[name='queryBeginRegisterDate']").val();
    var queryEndRegisterDate = $("#queryTeacherTable :text[name='queryEndRegisterDate']").val();
    var classzId = $("#queryTeacherTable select[name='queryClasszSelect']").val();
    var gotoPage = $("#queryTeacherTable input[type='hidden'][name='gotoPageHidden']").val();

    $.post("/teachermanager/queryTeacherPage?t=" + new Date().getTime(), {
        "teacherId": teacherId,
        "teacherName": teacherName,
        "queryBeginBirthday": queryBeginBirthday,
        "queryEndBirthday": queryEndBirthday,
        "sex": sex,
        "phone": phone,
        "email": email,
        "queryBeginRegisterDate": queryBeginRegisterDate,
        "queryEndRegisterDate": queryEndRegisterDate,
        "classzId": classzId,
        "gotoPage": gotoPage
    }, function (data) {
        var jsonObject = JSON.parse(data);
        if (jsonObject.responseCode == 500) {
            layer.msg(jsonObject.message);
        }
        if (jsonObject.responseCode == 501) {
            layer.msg("系统级异常：" + jsonObject.message);
        }
        if (jsonObject.responseCode == 200) {
            {
                var pageDataListTable = $("#pageDataListTable");
                $(pageDataListTable).find("tbody").empty();
                var queryPageDataList = jsonObject.responseData.queryPageDataList;
                if (queryPageDataList.length == 0 && gotoPage > 1) {//实现当前页删光显示前一页
                    $("#queryTeacherTable input[type='hidden'][name='gotoPageHidden']").val(gotoPage - 1);
                    fillPageDataListTable();
                }
                if (queryPageDataList.length == 0) {
                    $(pageDataListTable).append(createTableEmpty());
                } else {
                    for (var i = 0; i < queryPageDataList.length; i++) {
                        var newTR = createEachTeacherTR(queryPageDataList[i]);
                        $(pageDataListTable).find("tbody").append(newTR);
                    }
                }
            }

            {
                var queryGotoMap = jsonObject.responseData.queryGotoMap;
                var getAllPageRecordCount = queryGotoMap.getAllPageRecordCount;
                var getCurrentPageRecordCount = queryGotoMap.getCurrentPageRecordCount;
                var pageSize = queryGotoMap.pageSize;
                layui.use(['laypage'], function () {
                    var laypage = layui.laypage;
                    //分页导航自定义样式
                    laypage.render({
                        elem: 'pageComponent',
                        count: getAllPageRecordCount,
                        limit: pageSize,
                        groups: 20,
                        theme: '#1E9FFF',
                        curr: gotoPage,
                        jump: function (obj, first) {
                            if (!first) {
                                var clickGotoPage = obj.curr;//获取当前点击页码
                                $("#queryTeacherTable input[type='hidden'][name='gotoPageHidden']").val(clickGotoPage);
                                fillPageDataListTable();
                            }
                        }
                    });
                });

                $("#gotoPageTable button[name='getAllPageRecordCount']").text("所有页的总记录数：" + getAllPageRecordCount);
                $("#gotoPageTable button[name='getCurrentPageRecordCount']").text("当前页的记录数：" + getCurrentPageRecordCount);
            }
        }
    });
}

function showEditTeacherDialog() {
    var teacherId = $(this).attr("teacherId");

    $.post("/teachermanager/queryTeacherById?t=" + new Date().getTime(), {
        "teacherId": teacherId,
    }, function (data) {
        var jsonObject = JSON.parse(data);
        if (jsonObject.responseCode == 500) {
            layer.msg(jsonObject.message);
        }
        if (jsonObject.responseCode == 501) {
            layer.msg("系统级异常：" + jsonObject.message);
        }
        if (jsonObject.responseCode == 200) {
            var getTeacher = jsonObject.responseData;

            $("#editTeacherTable :text[name='teacherId']").val(getTeacher.teacherId);
            $("#editTeacherTable input[type='hidden'][name='id']").val(getTeacher.id);
            $("#editTeacherTable :text[name='teacherName']").val(getTeacher.teacherName);
            $("#editTeacherTable :text[name='password']").val(getTeacher.password);
            var birthdayDateObject = new Date(getTeacher.birthday);
            var showBirthdayString = birthdayDateObject.getFullYear() + "-" + birthdayDateObject.getMonth() + "-" + birthdayDateObject.getDate();
            $("#editTeacherTable :text[name='editBirthday']").val(showBirthdayString);
            $("#editTeacherTable select[name='sex']").val(getTeacher.sex);
            $("#editTeacherTable :text[name='phone']").val(getTeacher.phone);
            $("#editTeacherTable :text[name='email']").val(getTeacher.email);

            layer.open({
                type: 1,
                title: '编辑教师',
                area: ['800px'],
                content: $("#editTeacherDivOut"),
                success: function (layero, index) {
                    //alert(layero + "  " + index);
                }
            });
        }
    });
}

function deleteTeacher() {
    var teacherId = $(this).attr("teacherId");
    layer.open({
        type: 0,
        content: "确认删除教师？",
        btn: ['删除', '关闭'],
        yes: function (index, layero) {
            layer.msg("暂时不支持此功能！");
        }
    });
}

function createEachTeacherTR(eachTeacher) {
    var newTR = $("<tr></tr>");

    var id_td = $("<td></td>");
    $(id_td).css("width", "150px");
    $(id_td).text(eachTeacher.id);

    var teacherId_td = $("<td></td>");
    $(teacherId_td).text(eachTeacher.teacherId);

    var teacherName_td = $("<td></td>");
    $(teacherName_td).text(eachTeacher.teacherName);

    var birthday_td = $("<td></td>");
    $(birthday_td).text(eachTeacher.birthday);

    var sex_td = $("<td></td>");
    $(sex_td).text(eachTeacher.sex);

    var phone_td = $("<td></td>");
    $(phone_td).text(eachTeacher.phone);

    var email_td = $("<td></td>");
    $(email_td).text(eachTeacher.email);

    var registerDate_td = $("<td></td>");
    $(registerDate_td).text(eachTeacher.registerDate);

    var organizationName_td = $("<td></td>");
    $(organizationName_td).text(eachTeacher.organizationName);

    var classzName_td = $("<td></td>");
    $(classzName_td).text(eachTeacher.classzName);

    var operate_td = $("<td></td>");
    $(operate_td).css("width", "150px");
    var editTeacherA = $(`<button class="layui-btn layui-btn-sm layui-btn-normal"> <i class="layui-icon">&#xe642;</i></button>`);
    $(editTeacherA).attr("teacherId", eachTeacher.id);
    $(editTeacherA).click(showEditTeacherDialog);
    var deleteTeacherA = $(`<button class="layui-btn layui-btn-sm layui-btn-danger"><i class="layui-icon">&#xe640;</i></button>`);
    $(deleteTeacherA).attr("teacherId", eachTeacher.id);
    $(deleteTeacherA).click(deleteTeacher);
    $(operate_td).append(editTeacherA);
    $(operate_td).append(deleteTeacherA);

    $(newTR).append(id_td);
    $(newTR).append(teacherId_td);
    $(newTR).append(teacherName_td);
    $(newTR).append(birthday_td);
    $(newTR).append(sex_td);
    $(newTR).append(phone_td);
    $(newTR).append(email_td);
    $(newTR).append(registerDate_td);
    $(newTR).append(organizationName_td);
    $(newTR).append(classzName_td);
    $(newTR).append(operate_td);

    return newTR;
}

function insertTeacher() {
    var teacherId = $("#insertTeacherTable :text[name='teacherId']").val();
    var teacherName = $("#insertTeacherTable :text[name='teacherName']").val();
    var password = $("#insertTeacherTable :text[name='password']").val();
    var birthday = $("#insertTeacherTable :text[name='insertBirthday']").val();
    var sex = $("#insertTeacherTable select[name='sex'] option:selected").val();
    var phone = $("#insertTeacherTable :text[name='phone']").val();
    var email = $("#insertTeacherTable :text[name='email']").val();

    $.post("/teachermanager/insertTeacher?t=" + new Date().getTime(), {
        "teacherId": teacherId,
        "teacherName": teacherName,
        "password": password,
        "birthday": birthday,
        "sex": sex,
        "phone": phone,
        "email": email,
    }, function (data) {
        var jsonObject = JSON.parse(data);
        if (jsonObject.responseCode == 500) {
            layer.msg(jsonObject.message);
        }
        if (jsonObject.responseCode == 501) {
            layer.msg("系统级异常：" + jsonObject.message);
        }
        if (jsonObject.responseCode == 200) {
            $("#insertTeacherTable :text[name='teacherId']").val("");
            $("#insertTeacherTable :text[name='teacherName']").val("");
            $("#insertTeacherTable :text[name='password']").val("");
            $("#insertTeacherTable :text[name='insertBirthday']").val("");
            $("#insertTeacherTable :text[name='sex']").val("1");
            $("#insertTeacherTable :text[name='phone']").val("");
            $("#insertTeacherTable :text[name='email']").val("");

            layui.use('form', function () {
                var form = layui.form;
                form.render();
            });

            fillPageDataListTable();
            layer.closeAll('page');
        }
    });
}

function updateTeacher() {
    var teacherId = $("#editTeacherTable :text[name='teacherId']").val();
    var id = $("#editTeacherTable input[type='hidden'][name='id']").val();
    var teacherName = $("#editTeacherTable :text[name='teacherName']").val();
    var birthday = $("#editTeacherTable :text[name='editBirthday']").val();
    var sex = $("#editTeacherTable select[name='sex'] option:selected").val()
    var phone = $("#editTeacherTable :text[name='phone']").val();
    var email = $("#editTeacherTable :text[name='email']").val();

    $.post("/teachermanager/updateTeacher?t=" + new Date().getTime(), {
        "id": id,
        "teacherId": teacherId,
        "teacherName": teacherName,
        "birthday": birthday,
        "sex": sex,
        "phone": phone,
        "email": email
    }, function (data) {
        var jsonObject = JSON.parse(data);
        if (jsonObject.responseCode == 500) {
            layer.msg(jsonObject.message);
        }
        if (jsonObject.responseCode == 501) {
            layer.msg("系统级异常：" + jsonObject.message);
        }
        if (jsonObject.responseCode == 200) {
            fillPageDataListTable();
            layer.closeAll('page');
        }
    });
}

function showInsertTeacherDialog() {
    layer.open({
        type: 1,
        title: '添加教师',
        area: ['800px'],
        content: $("#insertTeacherDivOut"),
        success: function (layero, index) {
            $("#insertTeacherTable :text[name='teacherName']").val("");
            initOrganizationInfo();
        }
    });
}

function initOrganizationInfo() {
    $.post("/commons/getCookieLoginOrganizationInfo?t=" + new Date().getTime(), function (data) {
        var jsonObject = JSON.parse(data);
        if (jsonObject.responseCode == 500) {
            layer.msg(jsonObject.message);
        }
        if (jsonObject.responseCode == 501) {
            layer.msg("系统级异常：" + jsonObject.message);
        }
        if (jsonObject.responseCode == 200) {
            var cookieOrganizationInfo = jsonObject.responseData;

            var organizationId = cookieOrganizationInfo.organizationId;
            var organizationName = cookieOrganizationInfo.organizationName;

            $(":text[name='organizationName']").val(organizationName);
            $("input[type='hidden'][name='organizationId']").val(organizationId);
        }
    });
}

function createTableEmpty() {
    var newTR = $("<tr></tr>");
    var newTD = $("<td></td>");
    $(newTD).text("列表为空！");
    $(newTD).attr("colspan", 12);
    $(newTD).css("text-align", "center");
    $(newTR).append(newTD);
    return newTR;
}
